<?php

namespace Admin\Controller;
use Think\Controller;
use Common\Api\Category;
class MenuController extends CommonController{
	public function index() {
		$this->_list( M('Menu') );
		$this->display();
	}

	public function add() {
		if(IS_AJAX) {
			$db = D('Menu');
			if( !$db->create() ) {
				$this->error( $db->getError() );
			}else{
				if( !$db->add() ) {
					$this->error('新增失败！');
				}else{
					$this->success('新增成功！', U('index'));
				}
			}
		}else{
			$Category = new Category();
			$menu = M('Menu')->field('id,title,pid')->select();
			$this->menuList = $Category->unlimitedForLevel($menu);
			$this->display();
		}
	}

	public function edit() {
		if(IS_AJAX) {
			$db = D('Menu');
			if( !$db->create() ) {
				$this->error( $db->getError() );
			}else{
				if( !$db->where(array('id'=>I('post.id')))->save() ) {
					$this->error('更新失败！');
				}else{
					$this->success('更新成功！', U('index'));
				}
			}
		}else{
			$Category = new Category();
			$menu = M('Menu')->field('id,title,pid')->select();
			$this->menuList = $Category->unlimitedForLevel($menu);
			$this->info = M('Menu')->where(array('id'=>I('get.id')))->find();
			$this->display();
		}
	}
}